package com.zj.config;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class VelocityConfig {
    /**
     * 模板引擎主要用来做解析的
     * 解析vm页面，找出vm页面中需要注入的值，即vm页面中的占位符
     * @return
     * */
    @Bean
    public VelocityEngine velocityEngine() {
        VelocityEngine velocityEngine = new VelocityEngine();
        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityEngine.init();
        return velocityEngine;
    }

    /**
     * 模板上下文  相当于map  对应模板.vm文件中的值，
     * 可以向模板中设置值  存放需要注入的值与实际值
     * @return
     * */
    @Bean
    public VelocityContext velocityContext() {
        VelocityContext velocityContext = new VelocityContext();
        return velocityContext;
    }
    //用户登录模板
    @Bean
    public Template userLoginSuccessTemplate(@Autowired VelocityEngine velocityEngine) {
        Template template = velocityEngine.getTemplate("vms/useLoginSuccess.vm","utf-8");
        return template;
    }
    //注册发送验证码模板
    @Bean
    public Template registerVerifyCodeTemplate(@Autowired VelocityEngine velocityEngine) {
        Template template = velocityEngine.getTemplate("vms/registerVerifyCode.vm","utf-8");
        return template;
    }
    //订单成功模板
    @Bean
    public Template orderSuccessTemplate(@Autowired VelocityEngine velocityEngine) {
        Template template = velocityEngine.getTemplate("vms/orderSuccess.vm","utf-8");
        return template;
    }
    //订单状态更新模板
    @Bean
    public Template orderStatusUpdateTemplate(@Autowired VelocityEngine velocityEngine) {
        Template template = velocityEngine.getTemplate("vms/orderStatusSuccess.vm","utf-8");
        return template;
    }
}
